package com.zhkj.txg.module.order.model

import com.zhkj.lib.http.BaseManager
import com.zhkj.lib.http.BaseResponse
import com.zhkj.lib.http.ResponseSubscribeListener
import com.zhkj.lib.http.SubscribeListener
import com.zhkj.txg.module.order.entity.*
import io.reactivex.disposables.Disposable

class OrderManager : BaseManager<OrderApi>(OrderApi::class.java) {

    fun buyNow(
        token: String, goodsId: Int, number: Int, shopId: Int,
        listener: SubscribeListener<OrderSubmitDetailResponse>
    ): Disposable {
        val buyNow = apiService.buyNow(token, goodsId, number, shopId)
        return subscribe(buyNow, ResponseSubscribeListener(listener))
    }

    fun groupBuyNow(
        token: String,
        goodsId: Int,
        number: Int,
        specId: Int,
        group_id: Int = 0,
        type: Int = 1,
        shareId: Int = 0,
        listener: SubscribeListener<OrderSubmitGroupResponse>
    ): Disposable {
        val groupBuyNow =
            apiService.groupBuyNow(token, goodsId, number, specId, group_id, type, shareId)
        return subscribe(groupBuyNow, ResponseSubscribeListener(listener))
    }

    fun addressList(
        token: String,
        listener: SubscribeListener<AddressListResponse>
    ): Disposable {
        val addressList = apiService.addressList(token)
        return subscribe(addressList, ResponseSubscribeListener(listener))
    }

    fun addressDetail(
        token: String,
        id: Int,
        listener: SubscribeListener<AddressDetailResponse>
    ): Disposable {
        val addressDetail = apiService.addressDetail(token, id)
        return subscribe(addressDetail, ResponseSubscribeListener(listener))
    }

    fun addAddress(
        token: String,
        address: AddressDetailEntity,
        listener: SubscribeListener<BaseResponse>
    ): Disposable {
        val addAddress = apiService.addAddress(
            token, address.id, address.consignee, address.gender, address.mobile, address.province,
            address.city, address.district, address.address, if (address.isDefault) 1 else 0
        )
        return subscribe(addAddress, ResponseSubscribeListener(listener))
    }

    fun submitOrder(
        map: Map<String, String>,
        listener: SubscribeListener<OrderSubmitResponse>
    ): Disposable {
        val submit = apiService.submit(map)
        return subscribe(submit, ResponseSubscribeListener(listener))
    }


    fun submitGroupOrder(
        token: String,
        type: Int,//购买类型 0单买 1团购 //拼团ID
        goodsId: Int? = 0,
        number: Int,
        specId: String,
        group_id: Int,
        addressId: Int,
        couponId: Int,
        user_note: String,
        shareId: Int,
        listener: SubscribeListener<OrderSubmitResponse>
    ): Disposable {
        val submit = apiService.groupSubmit(
            token,
            type,
            goodsId,
            number,
            specId,
            group_id,
            addressId,
            couponId,
            user_note,
            shareId
        )
        return subscribe(submit, ResponseSubscribeListener(listener))
    }

    fun payOrder(
        map: Map<String, String>,
        listener: SubscribeListener<OrderPayResponse>
    ): Disposable {
        val payOrder = apiService.payOrder(map)
        return subscribe(payOrder, ResponseSubscribeListener(listener))
    }

    fun groupPayOrder(
        token: String, orderId: Long, orderSignNumber: String, payType: String,
        listener: SubscribeListener<OrderPayResponse>
    ): Disposable {
        val payOrder = apiService.groupPayOrder(token, orderId, orderSignNumber, payType)
        return subscribe(payOrder, ResponseSubscribeListener(listener))
    }

    fun cartBuy(
        cartIds: MutableList<String>, addressId: Int,shopId: String,
        listener: SubscribeListener<OrderSubmitDetailResponse>
    ): Disposable {
        val cartBuy = apiService.cartBuy(cartIds,shopId)
        return subscribe(cartBuy, ResponseSubscribeListener(listener))
    }

    fun orderList(
        token: String, page: Int, rows: Int, type: String,
        listener: SubscribeListener<OrderListResponse>
    ): Disposable {
        val orderList = apiService.orderList(token, page, rows, type)
        return subscribe(orderList, ResponseSubscribeListener(listener))
    }

    fun groupOrderList(
        token: String, page: Int, rows: Int, type: String,
        listener: SubscribeListener<OrderListResponse>
    ): Disposable {
        val orderList = apiService.groupOrderList(token, page, rows, type)
        return subscribe(orderList, ResponseSubscribeListener(listener))
    }

    fun orderCancel(
        token: String, orderId: Long,
        listener: SubscribeListener<OrderCancelResponse>
    ): Disposable {
        val orderCancel = apiService.orderCancel(token, orderId)
        return subscribe(orderCancel, ResponseSubscribeListener(listener))
    }

    fun groupOrderCancel(
        token: String, orderId: Long,
        listener: SubscribeListener<OrderCancelResponse>
    ): Disposable {
        val orderCancel = apiService.groupOrderCancel(token, orderId)
        return subscribe(orderCancel, ResponseSubscribeListener(listener))
    }

    fun orderDetail(
        token: String, orderId: Long,
        listener: SubscribeListener<OrderDetailResponse>
    ): Disposable {
        val orderDetail = apiService.orderDetail(token, orderId)
        return subscribe(orderDetail, ResponseSubscribeListener(listener))
    }

    fun groupOrderDetail(
        token: String, orderId: Long,
        listener: SubscribeListener<OrderDetailResponse>
    ): Disposable {
        val orderDetail = apiService.groupOrderDetail(token, orderId)
        return subscribe(orderDetail, ResponseSubscribeListener(listener))
    }

    fun orderShippingDetail(
        token: String, orderId: Long,
        listener: SubscribeListener<OrderShippingResponse>
    ): Disposable {
        val shippingDetail = apiService.shippingDetail(token, orderId)
        return subscribe(shippingDetail, ResponseSubscribeListener(listener))
    }

    fun groupShippingDetail(
        token: String, orderId: Long,
        listener: SubscribeListener<OrderShippingResponse>
    ): Disposable {
        val shippingDetail = apiService.groupShippingDetail(token, orderId)
        return subscribe(shippingDetail, ResponseSubscribeListener(listener))
    }

    fun receiveOrder(
        token: String, orderId: Long,
        listener: SubscribeListener<ReceiveOrderResponse>
    ): Disposable {
        val receiveOrder = apiService.receiveOrder(token, orderId)
        return subscribe(receiveOrder, ResponseSubscribeListener(listener))
    }

    fun groupReceiveOrder(
        token: String, orderId: Long,
        listener: SubscribeListener<ReceiveOrderResponse>
    ): Disposable {
        val receiveOrder = apiService.groupReceiveOrder(token, orderId)
        return subscribe(receiveOrder, ResponseSubscribeListener(listener))
    }

    fun refundGoods(
        token: String, orderId: Long, goodsId: Long,
        listener: SubscribeListener<RefundGoodsResponse>
    ): Disposable {
        val refundGoods = apiService.refundGoods(token, orderId, goodsId)
        return subscribe(refundGoods, ResponseSubscribeListener(listener))
    }

    fun groupRefundGoods(
        token: String, orderId: Long, goodsId: Long,
        listener: SubscribeListener<RefundGoodsResponse>
    ): Disposable {
        val refundGoods = apiService.groupRefundGoods(token, orderId, goodsId)
        return subscribe(refundGoods, ResponseSubscribeListener(listener))
    }

    fun refundSubmit(
        token: String, orderId: Long, GoodsId: Long, type: Int,
        goodsStatus: Int, reason: String, describe: String, images: String,
        listener: SubscribeListener<BaseResponse>
    ): Disposable {
        val refundSubmit = apiService.refundSubmit(
            token, orderId, GoodsId, type, goodsStatus,
            reason, describe, images
        )
        return subscribe(refundSubmit, ResponseSubscribeListener(listener))
    }

    fun groupRefundSubmit(
        token: String, orderId: Long, GoodsId: Long, type: Int,
        goodsStatus: Int, reason: String, describe: String, images: String,
        listener: SubscribeListener<BaseResponse>
    ): Disposable {
        val refundSubmit = apiService.groupRefundSubmit(
            token, orderId, GoodsId, type, goodsStatus,
            reason, describe, images
        )
        return subscribe(refundSubmit, ResponseSubscribeListener(listener))
    }

    fun refundDetail(
        token: String, refundId: Long, orderId: Long, goodsId: Long,
        listener: SubscribeListener<RefundDetailResponse>
    ): Disposable {
        val refundDetail = apiService.refundDetail(token, refundId, orderId, goodsId)
        return subscribe(refundDetail, ResponseSubscribeListener(listener))
    }

    fun groupRefundDetail(
        token: String, orderId: Long, goodsId: Long,
        listener: SubscribeListener<RefundDetailResponse>
    ): Disposable {
        val refundDetail = apiService.groupRefundDetail(token, orderId, goodsId)
        return subscribe(refundDetail, ResponseSubscribeListener(listener))
    }

    fun refundUndo(
        token: String, orderId: Long, goodsId: Long,
        listener: SubscribeListener<BaseResponse>
    ): Disposable {
        val refundUndo = apiService.refundUndo(token, orderId, goodsId)
        return subscribe(refundUndo, ResponseSubscribeListener(listener))
    }

    fun groupRefundUndo(
        token: String, orderId: Long, goodsId: Long,
        listener: SubscribeListener<BaseResponse>
    ): Disposable {
        val refundUndo = apiService.groupRefundUndo(token, orderId, goodsId)
        return subscribe(refundUndo, ResponseSubscribeListener(listener))
    }

    fun deliveryList(listener: SubscribeListener<DeliveryResponse>): Disposable {
        val deliveryList = apiService.getDeliveryList()
        return subscribe(deliveryList, ResponseSubscribeListener(listener))
    }

    fun submitDelivery(
        token: String, orderId: Long, goodsId: Long, code: String, shippingNo: String,
        listener: SubscribeListener<BaseResponse>
    ): Disposable {
        val submitDelivery = apiService.submitDelivery(token, orderId, goodsId, code, shippingNo)
        return subscribe(submitDelivery, ResponseSubscribeListener(listener))
    }

    fun groupSubmitDelivery(
        token: String, orderId: Long, goodsId: Long, code: String, shippingNo: String,
        listener: SubscribeListener<BaseResponse>
    ): Disposable {
        val submitDelivery = apiService.groupSubmitDelivery(token, orderId, goodsId, code, shippingNo)
        return subscribe(submitDelivery, ResponseSubscribeListener(listener))
    }

    fun submitEvaluate(
        token: String, orderId: Long, comment: String,
        listener: SubscribeListener<BaseResponse>
    ): Disposable {
        val submitEvaluate = apiService.submitEvaluate(token, orderId, comment)
        return subscribe(submitEvaluate, ResponseSubscribeListener(listener))
    }

    fun groupSubmitEvaluate(
        token: String, orderId: Long, goods_id: Long,
        goods_rank: Int, comment: String, images: String,
        listener: SubscribeListener<BaseResponse>
    ): Disposable {
        val submitEvaluate = apiService.groupSubmitEvaluate(token, orderId, goods_id, goods_rank, comment, images)
        return subscribe(submitEvaluate, ResponseSubscribeListener(listener))
    }

    fun deleteAddress(
        token: String, id: Long,
        listener: SubscribeListener<BaseResponse>
    ): Disposable {
        val deleteAddress = apiService.deleteAddress(token, id)
        return subscribe(deleteAddress, ResponseSubscribeListener(listener))
    }

    fun deleteOrder(
        token: String, orderId: Long,
        listener: SubscribeListener<ReceiveOrderResponse>
    ): Disposable {
        val deleteOrder = apiService.deleteOrder(token, orderId)
        return subscribe(deleteOrder, ResponseSubscribeListener(listener))
    }

    fun deleteGroupOrder(
        token: String, orderId: Long,
        listener: SubscribeListener<ReceiveOrderResponse>
    ): Disposable {
        val deleteOrder = apiService.deleteGroupOrder(token, orderId)
        return subscribe(deleteOrder, ResponseSubscribeListener(listener))
    }
}